﻿using App_Start;
using BLL;
using Microsoft.AspNetCore.Mvc;
using NetHelperCore;
using System.Diagnostics;

namespace ServiceManage.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [CheckLocalFilter]
    public class ConfigController : ControllerBase
    {

        /// <summary>
        /// 获取配置文件信息
        /// </summary>
        /// <returns></returns>
        public ServiceResultModel GetConfig()
        {
            ServiceResultModel _rm = new ServiceResultModel()
            {
                Success = -1,
                Result = null,
                ErrorMessage = ""
            };

            try
            {
                if (ConfigBLL.Config == null)
                {
                    ConfigBLL.LoadConfig();
                }

                if (ConfigBLL.Config != null)
                {
                    _rm.Success = 1;
                    _rm.Result = ConfigBLL.Config;
                }

            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogHelper.LogType.Error, ex.Message + "---" + ex.StackTrace, "", new StackTrace(new StackFrame(true)).GetFrame(0).GetMethod().Name);
                _rm.Success = 0;
                _rm.ErrorMessage = ex.Message;
            }


            return _rm;
        }

        /// <summary>
        /// 设置配置文件信息
        /// </summary>
        /// <returns></returns>
        public ServiceResultModel SetConfig()
        {
            ServiceResultModel _rm = new ServiceResultModel()
            {
                Success = -1,
                Result = null,
                ErrorMessage = ""
            };

            try
            {
                if (ConfigBLL.Config == null)
                {
                    ConfigBLL.LoadConfig();
                }

                if (ConfigBLL.Config != null)
                {
                    string _param = Request.Form["ParamStr"].ToString();
                    if (!string.IsNullOrWhiteSpace(_param))
                    {
                        JsonHelper _json = new JsonHelper(_param);
                        //ConfigBLL.ConfigBLL.Config.StartAutoRunMqtt = Convert.ToBoolean(_json.GetVar("StartAutoRunMqtt"));

                        ConfigBLL.SaveConfig();
                    }

                    _rm.Success = 1;
                }

            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogHelper.LogType.Error, ex.Message + "---" + ex.StackTrace, "", new StackTrace(new StackFrame(true)).GetFrame(0).GetMethod().Name);
                _rm.Success = 0;
                _rm.ErrorMessage = ex.Message;
            }


            return _rm;
        }

    }
}
